package org.rmm.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.rmm.db.Repository;
import org.rmm.model.Order;
import org.rmm.representation.OrderRepresentation;

/**
 * /GetOrders
 * @author jvelilla
 */

public class GetOrdersManagementService extends HttpServlet {

	private static final String APPLICATION_JSON = "application/json";
	private static final String APPLICATION_XML = "application/xml";
	
	private static final Logger LOG = Logger
			.getLogger(GetOrdersManagementService.class);
	private static final long serialVersionUID = 1L;

	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			LOG.info("Processing GET ORDERS");
			processGetOrders(resp);
		}catch (Exception e) {
			resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		 finally {
		}
	 }



	private void processGetOrders(HttpServletResponse resp) throws IOException {
		resp.setContentType(APPLICATION_XML);
		PrintWriter out = resp.getWriter();
		Collection<Order> orders = Repository.INSTANCE.getOrders();
		LOG.info("Processing response ["+ orders.toString() +" ]");
		out.print(new OrderRepresentation().asXML(orders));
		resp.setStatus(HttpServletResponse.SC_OK);
	}



	
	
}
